import util from "../../utils/util";
import network from "../../utils/network";

const app = getApp()
Page({
    data: {
        year: 2022,
        type: 2,
        yearData: {},
        active: 0,
        year_arr: [],
    },
    onLoad: function (options) {
        const now = new Date()
        var year = now.getFullYear();
        var type = now.getMonth() > 5 ? 2 : 1
        let arr = []
        for (let i = 1980; i <= year; i++) {
            arr.push({
                name: i
            })
        }
        this.setData({
            year_arr: arr,
            type: type
        })
        this.getData()
    },
    getData() {
        util.showLoading('加载中')
        network.reqPromise('GET', {
            uri: app.api.queryCompanyYearTargetSummary,
            params: {
                year: this.data.year,
                type: this.data.type
            }
        }).then(res => {
            if (res.data.month_target_list) {
                res.data.month_target_list.forEach(item => {
                    item.active = 0
                })
                this.setData({
                    active: 0
                })
            }
            this.setData({
                yearData: res.data
            })
            util.hideLoading()
        }).catch(err => {
            util.showToast(err.message ? err.message : '加载失败')
            util.hideLoading()
        })
    },
    onChange(e) {
        const month = e.detail.name
        this.setData({
            active: e.detail.index
        })
        this.selectComponent('#tabs' + month).resize();
    },
    onRadioChange(e) {
        this.setData({
            type: e.detail
        })
    },
    selectYearConfirm(e) {
        this.setData({
            year: e.detail.value.name
        })
    },
    search() {
        if (!this.data.year) {
            util.showToast('请选择年份')
            return
        }
        if (!this.data.type) {
            util.showToast('请选择年度')
            return
        }
        this.getData()
    },
});
